/*
 * NewClass.java
 *
 * Created on 7 de Janeiro de 2007, 23:48
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package controller.SGCA;

/**
 *
 * @author wldutra
 */

import java.sql.*;

public class RegNegocio {
    
    private Connection con;
    private Statement stm;
    private String conDriver;
    private String urlString;
    private String qry;
    
    /** Creates a new instance of NewClass */
    public RegNegocio() {
    }

    protected void setConDriver(String conDriver) {
        this.conDriver = conDriver;
    }

    protected void setUrlString(String urlString) {
        this.urlString = urlString;
    }

    protected String getQry() {
        return qry;
    }
    
    private int armaConexao() {
        int ret = 0;
        try{
            Class.forName( conDriver );
            con = DriverManager.getConnection(urlString);
            stm = con.createStatement();
        }
        catch( ClassNotFoundException e1 ){
            qry = e1.getMessage();
            ret = -1;
        }
        catch( Exception e2 ){
            qry = e2.getMessage();
            ret = -2;
        }
        return ret;
    }
    
    protected int getValor(String cmplQry){
        int ret = armaConexao();
        if( ret > -1 ){
            try {
                qry = "SELECT" + cmplQry;
                ResultSet res = stm.executeQuery(qry);
                res.next();
                ret = res.getInt(1);
            }
            catch(SQLException e){
                ret = -3;
            }
        }
        return ret;
    }
    
    protected int exeQuery(String stQuery){
        int ret = armaConexao();
        if (ret > -1 ){
            try{
                qry = stQuery;
                ret = stm.executeUpdate(qry);
            }
            catch ( SQLException e ){
                ret = -3;
            }
        }
        return ret;
    }
    
    protected String devStr( String cmp ){
        if( cmp != null ){
            cmp = cmp.replace("\'","'");
            cmp = "'" + cmp.replace( '\u0096','-' ) + "'";
        }else
            cmp = "null";
        return cmp;
    }
    
    protected String devInt( String cmp ){
        if(cmp.equals(""))
            return "null";
        else
            return cmp;
    }
     
}
